<template>
    <div>
        <el-row>
            <el-col :span="20" :offset="2">
                <el-row :gutter="24" style="border:1px solid #CCC;border-radius: 5px; padding: 20px;">
                    <el-col :span="8">
                        <el-upload class="avatar-uploader" action="api/image/upload" :show-file-list="false"
                            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                            <img :src="'api/' + userinfo.avatar" class="avatar">
                        </el-upload>
                    </el-col>
                    <el-col :span="16">
                        <el-row :gutter="24" style="margin-top:10px;">
                            <el-col :span="4">会员信息</el-col>
                            <el-col :span="4" :offset="16"><i class="el-icon-s-tools"></i></el-col>
                        </el-row>
                        <el-row :gutter="24" style="margin-top:10px;">
                            <el-row :gutter="24"></el-row>
                            <el-descriptions class="margin-top" title="" :column="3" :size="size" border>
                                <el-descriptions-item>
                                    <template slot="label">
                                        <i class="el-icon-user"></i>
                                        昵称
                                    </template>
                                    {{ userinfo.nickname }}
                                </el-descriptions-item>
                                <el-descriptions-item>
                                    <template slot="label">
                                        <i class="el-icon-mobile-phone"></i>
                                        手机号
                                    </template>
                                    {{ userinfo.phone }}
                                </el-descriptions-item>
                                <el-descriptions-item>
                                    <template slot="label">
                                        <i class="el-icon-message"></i>
                                        邮箱
                                    </template>
                                    {{ userinfo.email }}
                                </el-descriptions-item>
                            </el-descriptions>
                        </el-row>

                        <el-row :gutter="24" style="margin-top: 30px;">
                            <el-col :span="6">
                                <el-button type="info" plain>代付款</el-button>
                            </el-col>
                            <el-col :span="6">
                                <el-button type="info" plain>待收货</el-button>
                            </el-col>
                            <el-col :span="6">
                                <el-button type="info" plain>待评价</el-button>
                            </el-col>
                            <el-col :span="6">
                                <el-button type="info" plain>所有订单</el-button>
                            </el-col>
                        </el-row>
                    </el-col>
                </el-row>
                <el-row :gutter="24" style=" margin-top:20px;border:1px solid #CCC;border-radius: 5px;">
                    <el-col :span="8" v-for="(item, index) in addresslist">
                        <el-card shadow="hover" class='addrbox' :class="{ defaultaddress: item.isdefault }">
                            <div class="addrdiscover" style="position: relative;">
                                <el-row>
                                    <el-col style="font-size:26px">{{ item.name }}</el-col>
                                    <el-col style="margin:10px 0 ;">{{ item.receivername }} {{ item.phonenumber }}</el-col>
                                    <el-col>{{ item.province }}-{{ item.city }}-{{ item.district }} {{ item.detailedaddress
                                    }}</el-col>
                                </el-row>
                                <div class="cover">
                                    <el-row>
                                        <el-col :span="6" :offset="18">

                                            <i @click="setdefault(item, index)" style="cursor: pointer;"
                                                v-if="!item.isdefault" class="el-icon-star-off"></i>
                                            <i @click="editaddress(item, index)" style="cursor: pointer;" class="el-icon-setting"></i>
                                            <i @click="deleteaddress(item,index)" style="cursor: pointer;" class="el-icon-delete"></i>
                                        </el-col>
                                    </el-row>
                                </div>
                            </div>
                        </el-card>
                        
                    </el-col>
                    <el-col v-if="addresslist.length<6" :span="8" >
                        <el-card shadow="hover" class='addrbox' style="text-align: center;cursor: pointer;">
                            <i style="font-size: 100px; color: #CCC;" class="el-icon-plus"></i>
                        </el-card>
                    </el-col>
                    <!-- <el-col :span="8">
                        <el-card shadow="hover">
                            鼠标悬浮时显示
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover">
                            鼠标悬浮时显示
                        </el-card>
                    </el-col> -->
                </el-row>
            </el-col>
        </el-row>

        <!-- start 修改收货地址 弹出框 -->
<el-dialog title="收货地址" :visible.sync="dialogEditAddressVisible">
   <el-form :model="editaddressobj">
    <el-form-item label="别称" :label-width="80">
      <el-input v-model="editaddressobj.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="联系人" :label-width="80">
        <el-input v-model="editaddressobj.receivername" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" :label-width="80">
        <el-input v-model="editaddressobj.receivername" autocomplete="off"></el-input>
    </el-form-item>
  </el-form> 
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogEditAddressVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogEditAddressVisible = false">确 定</el-button>
  </div>
</el-dialog>
 <!-- end 修改收货地址 弹出框 -->
    </div>
</template>

<script>
import api from '@/api/';

export default {

    data() {
        return {
            userinfo: {
                avatar: "api/image/url/eeee.png",
                nickname: '小三三er'
            },
            addresslist: [],
            dialogEditAddressVisible:false,
            editaddressobj:{},
            province:[],
            city:[],
            district:[],
        }
    },
    methods: {
        async handleAvatarSuccess(ref, file) {
            //ref  第一个参数服务器回应的数据
            //保存该用户的头像地址到数据库中
            const result = await api.putForm("api/user/avatar", { avatar: ref.data });
            if (result.code == 200) {
                //提示上传成功
                this.$message({
                    message: '上传成功',
                    type: 'success'
                });
                //重新更新本地数据
                this.userinfo.avatar = ref.data;
            }

        },
        beforeAvatarUpload(item,index) { 
           
        },
        async setdefault(item, index) {
            this.$confirm('确认要设置此地址为默认地址吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {

                //发送请求将该地址设为默认
                const result = await api.putForm("api/user/address/default", { id: item.id });
                if (result.code = 200) {
                    //先循环所有的地址将地址isdefault设为0
                    this.addresslist.forEach((item) => {
                        item.isdefault = 0;
                    });
                    //在本地将改地址的isdefault设为默认1
                    this.addresslist[index].isdefault = 1;
                }

                this.$message({
                    type: 'success',
                    message: '设置成功!'
                });
            }).catch(() => {

            });





        },
       async editaddress(item,index) {  
            this.editaddressobj={...item}
            this.dialogEditAddressVisible=true;
         //获取所有的省级区划
            const area=this.getArea(100000);
            console.log(area);
            //this.province=
        },
        deleteaddress(item,index) {
            this.$confirm('确认要删除此地址吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                //发送请求将该地址设为默认
                const result = await api.del("api/user/address",item.id);
                if (result.code = 200) {
                    this.addresslist.splice(index,1);
                }
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {

            });

         },
         async getArea(code){
            const result= await api.get("https://restapi.amap.com/v3/config/district",{key:'d5e466c74f37e2b1f89163a68a161ea7',keywords:code});
            return result;
         }
    },
    async mounted() {
        const result = await api.get("api/user/userinfo");
        this.userinfo = result.data;

        const addrList = await api.get("api/user/address");
        this.addresslist = addrList.data;
    }
}

</script>
<style>
.cover {
    background-color: rgba(168, 218, 220, 0);
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    text-align: center;
    display: none;
}

.addrdiscover:hover .cover {
    height: 100%;
    display: block;
}

.defaultaddress {
    background-color: pink;
}

.addrbox {
    border: 1px solid #CCC;
    margin: 20px 10px;
    height:160px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>